 package com.oanda.blackberry.restapi;
 
 import com.oanda.blackberry.net.ConnectionManager;
 import com.oanda.blackberry.net.RestException;
 import com.oanda.blackberry.net.UrlEncoder;
 import com.oanda.blackberry.util.log.Logger;
 import java.io.IOException;
 
 public class Position extends Rest
 {
   static String previousResponse;
   static long maxTradeId = 0L;
 
   public String list(String sessionToken, String accountId)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"open_positions\" : [{ \"dir\": \"l\", \"symbol\": \"EUR/USD\", \"units\": 1000, \"avg_price\": 25.23 },{ \"dir\": \"s\", \"symbol\": \"USD/CAD\", \"units\": 10000, \"avg_price\": 325.56 }]}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/poll.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + "&max_trade_id=" + UrlEncoder.encode(Long.toString(maxTradeId), "");
 
       url = url + parameters;
     }
     catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
     maxTradeId = Long.parseLong(this.response.substring(this.response.lastIndexOf(':') + 1, this.response.lastIndexOf('}')));
 
     if ((!this.response.startsWith("{\"closed\":[],\"updated\":[],\"opened\":[]")) || (previousResponse == null))
     {
       url = ConnectionManager.getUrl() + "v1/position/list.json";
       try {
         String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "");
 
         url = url + parameters;
       }
       catch (IOException e) {
       }
       url = url + ConnectionManager.getConnectionSuffix();
 
       this.response = proceedConnection(url);
 
       if (this.error)
       {
         throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
       }
       Logger.log("positions.list response: " + this.response);
 
       previousResponse = this.response;
       return this.response;
     }
 
     return previousResponse;
   }
 
   public String close(String sessionToken, String accountId, String symbol, String optPrice)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"ids\" : [12345, 12346, 12347],\"symbol\" : \"EUR/USD\",\"total_units\": 1234,\"price\" : 1.2345}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/position/close.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (symbol != null ? "&symbol=" + UrlEncoder.encode(symbol, "") : "") + ((optPrice != null) && (optPrice.length() > 0) ? "&price=" + UrlEncoder.encode(optPrice, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error)
     {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("positions close response: " + this.response);
     return this.response;
   }
}

